package by.exposit.controller;

import by.exposit.domain.User;
import by.exposit.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by HaRiA on 26.07.2014.
 */
@Controller
@RequestMapping("")
public class LogInController {

    @Autowired
   private UserService userService;

    @RequestMapping("/landing-page")
     private String redirectAfterLogin(User user) {

           if(user.getIsAdmin() == 0 ){
        //return "redirect:/users/menu";
           return "users/menu";}
        else if(user.getIsAdmin() == 1) {
               //return "redirect:/
               return "users/admin/menu";
           }   else
           {
             // return  "redirect:/
             return "not_found_page";

           }
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
      public ModelAndView logIn(HttpSession session)
    {
        ModelAndView model = new ModelAndView();

        session.invalidate();
      model.setViewName("login");

    return model;
    }

    @RequestMapping(value = "/submit", method = {RequestMethod.GET, RequestMethod.POST })
  public  ModelAndView submit(@RequestParam(value = "username", required = false) String login,
               @RequestParam(value = "password", required = false) String password,
               HttpServletRequest request, HttpSession session) {

               ModelAndView model = new ModelAndView();
              User user = userService.returnUserByLoginPassword(login, password);
       if (user != null) {
            session.setAttribute("user",user );
            model.addObject("user",user);
           String page = redirectAfterLogin(user);
           session.setAttribute("prevpage",page);
           session.setAttribute("homepage",page);
            model.setViewName(page);

        }
       else
        {

            model.setViewName("not_found_page");
        }

               return  model;
        //return new ModelAndView(redirectAfterLogin(user));
    }
    @RequestMapping(value = "/logout")
    public ModelAndView logout(HttpSession session)
    {

       /* ModelAndView model = new ModelAndView();
        model.setViewName("login");     */

        session.setAttribute("user", null);
        session.invalidate();

        return  new ModelAndView("redirect:/login");
        // model;

    }
   @RequestMapping(value = "/registration",method = {RequestMethod.GET})
   public ModelAndView create(HttpSession session)
   {

       ModelAndView model = new ModelAndView();

       session.invalidate();
       model.setViewName("registration");
       return model;
   }

    @RequestMapping(value = "create", method = RequestMethod.POST)
      public ModelAndView create(@RequestParam(value = "name", required = true) String name,
                                 @RequestParam(value = "lname",required = true) String lname,
                                 @RequestParam(value = "login",required = true) String login,
                                 @RequestParam(value = "password", required = false) String password,
                                 @RequestParam(value = "email",required = true) String email,
                                 HttpServletRequest request, HttpSession session) //Crate new User in system with non Admin rights
    {



       User user = new User(name, lname,login,password,email);

        userService.saveUser(user);

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.setViewName("login");
        return modelAndView;

    }



   }
